﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.QLMS.Component.SystemSettingComponent;
using ZDSoft.QLMS.Entity.SystemSetting;
using ZDSoft.QLMS.Web.Helpers;
using NHibernate.Criterion;

namespace ZDSoft.QLMS.Web.Controllers
{
    public class AccountController : Controller
    {
        UserComponent userComponent = new UserComponent();

        public ActionResult Login()
        {
            return View();
        }

        public string AJAXLogin(string account, string password, string checkCode)
        {
            User user = userComponent.Login(account, password);
            if (user != null)
            {
                if (checkCode == Session["ValidateCode"].ToString())
                {
                    AppHelper.LoginedUser = user;
                    IList<Role> roles=user.Roles;
                    IList<SystemFunction> systemFunctions=new List<SystemFunction>();
                    foreach(Role role in roles)
                    {
                        foreach(SystemFunction systemFunction in role.SystemFunctions)
                        {
                        systemFunctions.Add(systemFunction);
                        }
                    }
                    AppHelper.Privileges=systemFunctions;
                    return "success";
                }
                else
                {
                    return "验证码错误！";
                }
            }
            else
            {
                return "用户名或密码错误或该用户名暂未激活！";
            }
        }

        public ActionResult UpdatePwd()
        {
            return View();
        }

        [HttpPost]
        public void UpdatePwd(string newpassword)
        {
            User user = AppHelper.LoginedUser;
            user.Password = newpassword;
            user.Update();
            AppHelper.LoginedUser = null;
        }

        public ActionResult ResetPwd()
        {
            return View();
        }

        [HttpPost]
        public string ResetPwd(string account)
        {
            IList<ICriterion> conditions = new List<ICriterion>();
            conditions.Add(Expression.Eq("Account", account));
            IList<User> users = userComponent.Find(conditions);
            if (users.Count == 0)
            {
                return "未注册的账户！";
            }
            else
            {
                users[0].IsReset = true;
                users[0].IsActive = false;
                users[0].Update();
                return "success";
            }
        }

        public ActionResult Logout()
        {
            AppHelper.LoginedUser = null;
            return View();
        }

        public string IsOwner(string oldPassword)
        {
            if (oldPassword == AppHelper.LoginedUser.Password)
            {
                return "success";
            }
            else
            {
                return "forbidden";
            }
        }
    }
}
